/**
 * 动态围墙
 * @copyright 
 * @author 
 */
import * as mars3d from "mars3d"
const Cesium = mars3d.Cesium

let map: mars3d.Map // 地图对象
let graphicLayer: mars3d.layer.GraphicLayer
// 初始化当前业务
export function onMounted(mapInstance: mars3d.Map): void {
  map = mapInstance // 记录map

  // 创建Graphic图层
  graphicLayer = new mars3d.layer.GraphicLayer({
    // name: this.config.name,
    pid: 99 // 图层管理 中使用，父节点id
  })
  map.addLayer(graphicLayer)

  addGraphicCs()

  
}

// 释放当前业务
export function onUnmounted(): void {
  map.removeLayer(graphicLayer)
  map = null
}
const boundarypath : string = import.meta.env.VITE_APP_BOUNDARY_PATH as string

function addGraphicCs() {

  mars3d.Util.fetchJson({ url: boundarypath }).then((geojson: any) => {
    const arr = mars3d.Util.geoJsonToGraphics(geojson) // 解析geojson
    for (let i = 0; i < arr.length; i++) {
      const item = arr[i]
      const primitive = new mars3d.graphic.WallPrimitive({
        positions: item.positions,
        style: {
          diffHeight: 100,
          material: mars3d.MaterialUtil.createMaterial(mars3d.MaterialType.LineFlow, {
            image: "./img/textures/fence.png",
            color: "#019df0",
            repeat: new Cesium.Cartesian2(5, 1),
            axisY: true, // 方向，true时上下，false左右
            speed: 2 // 速度，建议取值范围1-100
          })
        },
        attr: item.attr
      })
      graphicLayer.addGraphic(primitive)

    }
  })
}
